// pages/reg/reg.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  handle_name(e) {
    this.setData({ nickName: e.detail.value })
  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail
    this.setData({
      avatarUrl,
    })
  },
  async add_img() {
    return await wx.cloud.uploadFile({
      cloudPath: "img/" + new Date().getTime() + "-" + Math.floor(Math.random() * 1000),
      filePath: this.data.avatarUrl
    }).then(res => {
      return res.fileID
    })
  },

  certain() {
    if (!this.data.avatarUrl || !this.data.nickName) {
      wx.showToast({
        title: '请完善信息',
        icon: "none"
      })
    }
    else {
      wx.showLoading({
        success: res => {
          this.add_img().then(avatarUrl => {
            console.log(avatarUrl);
            let data = {
              avatarUrl,
              nickName: this.data.nickName,
              reg_time: new Date()
            }
            app.get('add_user', data).then(res => {
              wx.hideLoading()
              wx.showToast({
                title: res.msg,
                icon: "none"
              })
              wx.setStorageSync('args', res.data.args)
              setTimeout(() => {
                wx.switchTab({
                  url: '/pages/home/home',
                })
              }, 1000);
            })
          })

        }
      })
    }
  },
  handle_number(e){
    this.setData({ phoneNumber: e.detail.value })
  },
  handle_password(e){
    this.setData({ password: e.detail.value })
  },
  login(){
    let _this = this
    var appInstance = getApp()
    if(!this.data.phoneNumber){
      wx.showToast({
        title: '请输入手机号',
        icon: "none"
      })
      return
    }
    if(!this.data.password){
      wx.showToast({
        title: '请输入密码',
        icon: "none"
      })
      return
    }
    wx.showLoading({
      success: res => {
        wx.request({
          url: appInstance.globalData.requestUrl+'/wx/user/login',
          data: {
            "phone": _this.data.phoneNumber, //手机号
            "password": _this.data.password //密码
          },
          method:'POST',
          header: {
            'content-type': 'application/json' // 默认值
          },
          success (res) {
            if(res.data.code == '200'){
              wx.hideLoading()
              wx.showToast({
                title: '登录成功',
                icon: "success"
              })
              wx.setStorageSync('args', res.data.data)
              setTimeout(() => {
                wx.switchTab({
                  url: '/pages/home/home',
                })
              }, 1000);
            }else{
              wx.hideLoading()
              wx.showToast({
                title: res.data.msg,
                icon: "none"
              })
            }
            console.log(res.data)
          }
        })
      }
    })

  },
  register(){
    wx.navigateTo({
      url: '/pages/user/register/register',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
